package com.alan.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 打jar跑 hadoop jar mr.jar com.alan.mr.JobRun
 * Created by Alan on 2017/9/29.
 */
public class JobRun {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        conf.set("mapred.job.tracker","node1:9001");
        // 创建job对象
        Job job = Job.getInstance(conf);
        // 指定程序的入口
        job.setJarByClass(JobRun.class);
        job.setMapperClass(WcMapper.class);
        job.setReducerClass(WcReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //job.setNumReduceTasks(1);//设置reduce任务的个数，默认为1个

        //mapreduce 输入数据所丰目录或者文件 HDFS
        FileInputFormat.setInputPaths(job, new Path("/tmp/words.txt"));
        //mr执行后的输出数据的目录
        FileOutputFormat.setOutputPath(job, new Path("/tmp/wordsResult"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

}
